package com.gulimall.coupon.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

/**
 * <p>Seata Feign Request Interceptor</p>
 *
 * @author liuyang
 * @since 2021/8/21 10:10:10
 */
@Component @ConditionalOnClass({RequestInterceptor.class, GlobalTransactional.class}) public class SeataInterceptor
        implements RequestInterceptor {
    @Override public void apply(RequestTemplate request) {
        String xid = RootContext.getXID();
        if (StringUtils.isNotEmpty(xid)) {
            request.header(RootContext.KEY_XID, xid);
        }
    }
}
